package com.qeeyou.apps.android.craftsman.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;

public class EditInfoActivity extends Activity {

    private ImageView btnBack;
    private TextView txtvTitle;
    private EditText editInfo;
    private Button btnConfirm;
    private String title;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);

        setView();
        setEvent();

        Intent intent = getIntent();
        if(intent != null) {
            title = intent.getStringExtra("edit_title");
            content = intent.getStringExtra("edit_content");
            txtvTitle.setText(title);
            editInfo.setText(content);
            if(content == null || content.trim().equalsIgnoreCase("")) {
                editInfo.setSelection(0);
            } else {
                editInfo.setSelection(content.length());
            }
        }
    }

    private void setView() {
        btnBack = (ImageView) this.findViewById(R.id.btnBack4EditInfo);
        txtvTitle = (TextView) this.findViewById(R.id.txtvTitle4EditInfo);
        editInfo = (EditText) this.findViewById(R.id.editContent4EditInfo);
        btnConfirm = (Button) this.findViewById(R.id.btnConfirm4EditInfo);
    }

    private void setEvent() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(title == null || title.trim().equalsIgnoreCase("")) {

                } else if(title.equalsIgnoreCase("姓名")) {
                    Intent intent = new Intent();
                    EditInfoActivity.this.setResult(RESULT_CANCELED, intent);
                } else if(title.equalsIgnoreCase("居住地址")) {
                    Intent intent = new Intent();
                    EditInfoActivity.this.setResult(RESULT_CANCELED, intent);
                }
                EditInfoActivity.this.finish();
            }
        });

        btnConfirm.setOnClickListener(onConfirmClickListener);
    }

    private View.OnClickListener onConfirmClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String contentStr = editInfo.getText().toString();
            if(title == null || title.trim().equalsIgnoreCase("")) {

            } else if(title.equalsIgnoreCase("姓名")) {
                if(contentStr == null || contentStr.trim().equalsIgnoreCase("")) {
                    Toast.makeText(EditInfoActivity.this, "请添加用户姓名", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("user_name", contentStr);
                    EditInfoActivity.this.setResult(RESULT_OK, intent);
                }
            } else if(title.equalsIgnoreCase("居住地址")) {
                if(contentStr == null || contentStr.trim().equalsIgnoreCase("")) {
                    Toast.makeText(EditInfoActivity.this, "请添加居住地址", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("user_address", contentStr);
                    EditInfoActivity.this.setResult(RESULT_OK, intent);
                }
            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    EditInfoActivity.this.finish();
                }
            }, 600);
        }
    };
}
